package com.sunyur.tailor.channel.service.middle.receiveorder.impl;

import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.common.middle.base.ChannelDataResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelDataResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.receiveorder.model.ReceiveHeadModel;
import com.sunyur.tailor.channel.service.inner.receiveorder.model.ReceiveOrderListItemModel;
import com.sunyur.tailor.channel.service.inner.receiveorder.param.ReceiveHeadParam;
import com.sunyur.tailor.channel.service.inner.receiveorder.param.ReceiveOrderListItemParam;
import com.sunyur.tailor.channel.service.inner.receiveorder.service.SunyurReceiveOrderService;
import com.sunyur.tailor.channel.service.middle.receiveorder.MiddleReceiveOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName
 * @Description 收货单业务接口实现
 * @Author XuHui
 * @Date 2021/7/7 14:37
 * @Version 1.0
 */
@Service("middleReceiveOrderService")
public class MiddleReceiveOrderServiceImpl implements MiddleReceiveOrderService {

    private final static Logger LOGGER_INFO = LoggerFactory.getLogger(MiddleReceiveOrderServiceImpl.class);
    private final static Logger LOGGER_ERROR = LoggerFactory.getLogger(MiddleReceiveOrderServiceImpl.class);


    @Autowired
    private SunyurReceiveOrderService sunyurReceiveOrderService;

    @Override
    public ChannelResult<Boolean> create(MsgResultCode msgResultCode) throws Exception {
        //查询头和行信息
        ChannelDataResult dataResult = getHeadAndItem(msgResultCode);
        if (!ChannelDataResultUtil.isSuccess(dataResult)) {
            return ChannelResultUtil.error(dataResult.getCode(), dataResult.getMessage());
        }
        //业务处理
        ReceiveHeadModel receiveHead = (ReceiveHeadModel) dataResult.getHead();
        List<ReceiveOrderListItemModel> receiveOrderItem = (List<ReceiveOrderListItemModel>) dataResult.getItem();

        return ChannelResultUtil.success(true);

    }

    /**
     * @return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @Author XuHui
     * @Description 查询头和行信息
     * @Date 13:56 2021/12/28
     * @Param [msgResultCode]
     **/
    private ChannelDataResult getHeadAndItem(MsgResultCode msgResultCode) throws Exception {
        //获取收货单号
        String receiveOrderCode = msgResultCode.getCode();
        //查询收货头
        ReceiveHeadParam receiveHeadParam = new ReceiveHeadParam();
        receiveHeadParam.setReceiveOrderCode(receiveOrderCode);
        ChannelResult<ReceiveHeadModel> ReceiveHeadModelResult = sunyurReceiveOrderService.getReceiveOrder(receiveHeadParam);
        if (!ChannelResultUtil.isSuccess(ReceiveHeadModelResult)) {
            return ChannelDataResultUtil.error(ReceiveHeadModelResult.getCode(), ReceiveHeadModelResult.getMessage());
        }

        //查询收货单行
        ChannelResult<List<ReceiveOrderListItemModel>> ReceiveOrderListItemModelResult = sunyurReceiveOrderService.getReceiveListItem(new ReceiveOrderListItemParam(receiveOrderCode));

        if (!ChannelResultUtil.isSuccess(ReceiveOrderListItemModelResult)) {
            return ChannelDataResultUtil.error(ReceiveOrderListItemModelResult.getCode(), ReceiveOrderListItemModelResult.getMessage());
        }

        return ChannelDataResultUtil.success(ReceiveHeadModelResult.getContent(), ReceiveOrderListItemModelResult.getContent());
    }

}
